package com.ruoyi.mayi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.mayi.annotation.ApiLog;
import com.ruoyi.mayi.service.ApiLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * api
 *
 * @author yacong_liu
 * @version 1.0
 * @classname IndexController
 * @date 2022/2/7
 **/
@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {

    @Autowired
    private ApiLoginService apiLoginService;

    /**
     * 测试接口
     * @return ajaxResult
     */
    @RequestMapping("index")
    @ApiLog(isSaveResponseData = true)
    public AjaxResult index() {
        return success("com.ruoyi.mayi.controller.ApiController is success!");
    }

    /**
     * 获取token
     *
     * @param loginBody 请求参数
     * @return token
     */
    @PostMapping("token")
    @ApiLog(value = "获取token", isSaveResponseData = true)
    public AjaxResult apiLogin(@RequestBody LoginBody loginBody) {
        String token = apiLoginService.apiLogin(loginBody.getUsername(), loginBody.getPassword());

        AjaxResult ajaxResult = success("获取Token成功！");
        ajaxResult.put("token", token);
        ajaxResult.put("remark", "token有效期30分钟！");
        return ajaxResult;
    }
}
